package io.renren.modules.gengyun.service.impl;

import io.renren.modules.gengyun.entity.spare.CycleOfIndicator;
import io.renren.modules.gengyun.entity.spare.IndicatorShow;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.gengyun.dao.IndicatorDao;
import io.renren.modules.gengyun.entity.IndicatorEntity;
import io.renren.modules.gengyun.service.IndicatorService;

@Slf4j
@Service("indicatorService")
public class IndicatorServiceImpl extends ServiceImpl<IndicatorDao, IndicatorEntity> implements IndicatorService {

    @Autowired
    IndicatorDao indicatorDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<IndicatorEntity> page = this.page(
                new Query<IndicatorEntity>().getPage(params),
                new QueryWrapper<IndicatorEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<IndicatorShow> indicatorShows(String cropsRotationPlotId) {
        return indicatorDao.indicatorShows(cropsRotationPlotId);
    }


    @Override
    public List<IndicatorShow> findByIndicatorIdAndCropRotationId(String cropsRotationPlotId, String indicatorId, Long beginTime, Long endTime) {
        
        return indicatorDao.findByIndicatorIdAndCropRotationId(cropsRotationPlotId,
                indicatorId,
                beginTime,
                endTime);
    }
//
//    @Override
//    public Map<String, Double> getCycleOfIndicatorsByCropRotationId(String cropsRotationPlotId, Long beginTime, Long endTime) {
//        List<CycleOfIndicator> cycle = indicatorDao.getCycleOfIndicatorsByCropRotationId(cropsRotationPlotId, beginTime, endTime);
//        Map<String, Double> m = new HashMap<>();
//
//        Long startTimeStamp = 1592150400000L;
//        Long nowTimeStamp = System.currentTimeMillis();
//
//        if (beginTime != null) {
//            startTimeStamp = beginTime;
//        }
//         if(endTime != null) {
//             nowTimeStamp = endTime;
//         }
//
//         Long day = (nowTimeStamp - startTimeStamp) / (1000 * 60 * 60 * 24);
//
//        cycle.forEach(x -> {
//            m.put(x.getIndicatorId(), Double.parseDouble(String.valueOf((x.getN() * 7.0) / day)));
////            log.info(String.valueOf(m.get(x.getIndicatorId())));
//        });
//        return m;
//    }


    @Override
    public List<Map> getIndicatorsByTime(String cropsRotationPlotId,
                                 String indicatorType,
                                 Long beginTime,
                                  Long endTime) {
        return indicatorDao.getIndicatorsByTime(cropsRotationPlotId,indicatorType,beginTime,endTime);
    }

//    @Override
//    public Map<String, List<IndicatorShow>> CompareAllYearsDataByTargetName(String cropsRotationPlotId, String targetName, Long beginTime, Long endTime) {
//        return indicatorDao.CompareAllYearsDataByTargetName(cropsRotationPlotId,targetName,beginTime,endTime);
//    }

    @Override
    public List<Map<String, Object>> compareAllYearsDataByTargetName(String cropsRotationPlotId, String targetName, Long beginTime, Long endTime) {
        return indicatorDao.compareAllYearsDataByTargetName(cropsRotationPlotId,targetName,beginTime,endTime);
    }


//    @Override
//    public List<Map<String, IndicatorShow>> CompareIndicatorIdAndCropRotationId(String cropsRotationPlotId, String IndicatorId, String beginTime, String endTime) {
//        return indicatorDao.findCompareByIndicatorIdAndCropRotationId(cropsRotationPlotId,
//                 IndicatorId,
//                Long.valueOf(beginTime),
//                Long.valueOf(endTime));
//    }

//    @Override
//    public Map<String,List<IndicatorShow>> CompareIndicatorIdAndCropRotationId(String cropsRotationPlotId, String IndicatorId, String beginTime , String endTime) throws ParseException {
////        Map<String,List<IndicatorShow>> Indicator_map = new HashMap<>();
////        for (int i=0;i<Compare_years.length;){
////            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
////            Date date11 = simpleDateFormat.parse(Compare_years[i]);
////            long start_time = date11.getTime();
////            Calendar cal = Calendar.getInstance();
////            cal.setTime(date11);
////            cal.add(Calendar.YEAR, 1);
////            Date date12 = cal.getTime();
////            long end_time = date12.getTime();
////            List<IndicatorShow> in_list=indicatorDao.findByIndicatorIdAndCropRotationId(cropsRotationPlotId,IndicatorId,start_time,end_time);
////            Indicator_map.put(Compare_years[i],in_list);
////            i++;
////        }
////        return Indicator_map;
//    }

    @Override
    public List<String> getCropRotationId() {
        return indicatorDao.getCropRotationId();
    }


}